using UnityEngine;
using System.Collections;


public class bosspieces : MonoBehaviour
{
	//public GameObject bossBody;
	private GameObject mainGuns;
	GameObject rocketLaunchers ;
	GameObject shieldBoosters ;
	GameObject	enemySpawner ;
	GameObject	ionBeam ;
	public GameObject endBossMainAmmo ;
	public GameObject endBossRocketLaunchers ;
	public GameObject clusterBombLaunchers;
	GameObject endBossShieldBoosters ;
	GameObject	endBossEnemySpawner ;
	GameObject	endBossIonBeam ;
    public float fireRate = .5f;
	public float rocketSpeed = 1.4f;
    private float mTimer = 0;
    private GameObject player;
    private LineRenderer lineRenderer;
	public float bulletLifeTime;
	GameObject Boss3;
	private float mClusterTimer = 0;
    public static int percentage = 100;
	private GameObject clusters;
    public GUIStyle style;
	// Use this for initialization
	GameObject city;
	private HealthBar initialHBar;
	public Material yellowMat ;
	void Start ()
    {
		yellowMat.shader = Shader.Find("Transparent/Diffuse");
		initialHBar = transform.GetChild(0).gameObject.GetComponent<HealthBar>();
		city = GameObject.FindGameObjectWithTag("city").transform.GetChild(0).gameObject;
        player = GameObject.FindGameObjectWithTag("Player");
        lineRenderer = transform.GetChild(8).gameObject.GetComponent<LineRenderer>();
        lineRenderer.enabled = false;

    }
    void Update()
    {
        if(transform.childCount>0)
            lineRenderer.SetPosition(0, new Vector3(transform.position.x, transform.position.y + .75f, 0));
		clusters = GameObject.FindGameObjectWithTag("cluster");
		mClusterTimer += Time.deltaTime;
        if (mTimer > fireRate)
        {
            Fire();
            mTimer = 0.0f;
        }

        if (transform.childCount <= 5)
        {
            foreach (Transform child in transform)
            {

                if (child.name == "boss_piece2")
                {
					child.GetComponent<HealthBar>().enabled = true;

                    child.collider.isTrigger = false;
					child.renderer.material = yellowMat;
                    if(child.GetComponent<BossCanTakeDamage>() == null)
                        child.gameObject.AddComponent<BossCanTakeDamage>();
                }
            }
        }
        if (transform.childCount <= 3)
        {
            foreach (Transform child in transform)
            {

                if (child.name == "boss_piece3")
                {
					child.GetComponent<HealthBar>().enabled = true;
					child.renderer.material = yellowMat;
                    child.collider.isTrigger = false;
                    if (child.GetComponent<BossCanTakeDamage>() == null)
                        child.gameObject.AddComponent<BossCanTakeDamage>();
                }
				if(mClusterTimer > 2f && clusters == null)
				{
	            	ShieldBoosters();
					mClusterTimer = 0;
				}
            }
        }
        if (transform.childCount == 1)
        {
            lineRenderer.enabled = true;
            foreach (Transform child in transform)
            {
				child.GetComponent<HealthBar>().enabled = true;

				child.renderer.material = yellowMat;
                child.collider.isTrigger = false;
                if (child.GetComponent<BossCanTakeDamage>() == null)
                    child.gameObject.AddComponent<BossCanTakeDamage>();
            }
            FireBeam();
        }
        if (transform.childCount == 0)
        {
            PauseMenu.enemiesKilled++;
            transform.parent.gameObject.AddComponent<BossCanTakeDamage>();

            if (PlayerPrefs.GetString("Diffculty") != "Boss")
            {
                city.SetActive(true);
            }
            transform.parent.gameObject.GetComponent<BossCanTakeDamage>().Die();
            //Destroy(transform.parent.gameObject);
        }
        mTimer += Time.deltaTime;
    }
    void Fire()
    {
        FireMainGuns();

        if (transform.childCount <= 5 && transform.childCount > 3)
        {
            FireRockets();
        }
         if(transform.childCount <= 3)
        {

        }
        if (transform.childCount == 1)
         {
        //    print("i get here");
        //    FireBeam();
             print("rocket?");
             FireRockets();
        }

    }

    void FireMainGuns()
    {

        foreach (Transform child in transform)
        {
            if (child.tag == "BossPiece1")
            {
                GameObject mainGunFire = Instantiate(endBossMainAmmo, child.position, Quaternion.Euler(new Vector3(90, 90, 270))) as GameObject;
                // mainGunFire.transform.Translate( Vector3.down*10f * Time.deltaTime);
                //mainGunFire.rigidbody.AddForce(Vector3.Cross(new Vector3(270, 0, 0), Vector3.down).normalized * 200f);
                mainGunFire.rigidbody.mass = 1f;
                mainGunFire.rigidbody.AddForce(Vector3.down * 50);
                mainGunFire.rigidbody.mass = .00001f;
            }
        }
    }
	


	
//	void ShootShotty()
//	{

//		shottyTimer = 0.0f;
//	}
	void FireRockets (){
        foreach (Transform child in transform)
        {
            if (child.tag == "BossPiece2")
            {
				Rocket b = ((GameObject)Instantiate(endBossRocketLaunchers,child.transform.position,
	                Quaternion.Euler(new Vector3(270+180, 90, 270)))).GetComponent<Rocket>();
				b.fireDirection = Vector3.down;
				b.destVector = child.transform.position - player.transform.position;
				b.rocketSpeed = rocketSpeed;
            }
            if (child.tag == "BossPiece5")
            {
                Rocket a = ((GameObject)Instantiate(endBossRocketLaunchers, child.transform.position + new Vector3(-.25f, 0f, 0f), Quaternion.Euler(new Vector3(270 + 180, 90, 270)))).GetComponent<Rocket>();
                a.fireDirection = Vector3.down;
                a.destVector = child.transform.position - player.transform.position;
                a.rocketSpeed = rocketSpeed;

                Rocket b = ((GameObject)Instantiate(endBossRocketLaunchers, child.transform.position + new Vector3(.25f,0f,0f),Quaternion.Euler(new Vector3(270 + 180, 90, 270)))).GetComponent<Rocket>();
                b.fireDirection = Vector3.down;
                b.destVector = child.transform.position - player.transform.position;
                b.rocketSpeed = rocketSpeed;
            }
        }

	}

	void ShieldBoosters ()
	{
		        foreach (Transform child in transform)
        {
            if (child.tag == "BossPiece3")
            {
				
				
                GameObject clusterBombFire = Instantiate(clusterBombLaunchers, child.position, Quaternion.Euler(new Vector3(90, 180, 0))) as GameObject;
                clusterBombFire.rigidbody.AddForce(clusterBombFire.transform.forward * 50);
            }
        }

        //GameObject shieldBoosterDefense = Instantiate (endBossShieldBoosters, transform.position, Quaternion.identity) as GameObject;

	}

	void Spawner (){

        //GameObject enemySpawnerModule = Instantiate (endBossEnemySpawner, transform.position, Quaternion.identity) as GameObject;

	}

	void FireBeam ()
    {
        Vector3 lineLocation = new Vector3(Mathf.PingPong(Time.time*6, WithinBounds.Bounds.maxX * 4) - WithinBounds.Bounds.maxX*2, WithinBounds.Bounds.minY, 0);

        lineRenderer.SetPosition(1, lineLocation);
        RaycastHit hit;
        if (Physics.Raycast(transform.position, lineLocation - transform.position, out hit) && lineRenderer.enabled)
        {
            switch (hit.transform.gameObject.tag)
            {
                case "Player":
                    //Output message
                    //print("deal damage");
                    player.GetComponent<CanTakeDamage>().TakeDamage(10);
                    break;
            }
        }

	}
  void OnGUI()
    {
//        GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 350, 60), "Final Boss: " + percentage.ToString()+"%", style);
    }
	
}
